package com.example.demo.controller;

import com.example.demo.model.Song;
import com.example.demo.response.CommonReturnType;
import com.example.demo.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/song")
public class SongController {

  @Autowired
  private SongService songService;

  @PostMapping("/add")
  public CommonReturnType add(@RequestBody Song song) {
    songService.save(song);
    return CommonReturnType.success();
  }

  @GetMapping("/getList")
  public CommonReturnType getList(@RequestParam(value = "name", required = false) String name,
                                  @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                  @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
    return CommonReturnType.success(songService.getList(name, currentPage, limit));
  }

  @PostMapping("/update")
  public CommonReturnType update(@RequestBody Song song) {
    songService.update(song);
    return CommonReturnType.success();
  }

  @PostMapping("/delete")
  public CommonReturnType delete(@RequestParam(value = "id") Integer id) {
    songService.delete(id);
    return CommonReturnType.success();
  }

  @GetMapping("/getDetail")
  public CommonReturnType getDetail(@RequestParam(value = "id") Integer id) {
    Map<String, Object> map = new HashMap<>();
    map.put("song", songService.getDetail(id));
    return CommonReturnType.success(map);
  }


}
